<?php
namespace app\modules\common\components;

use ReflectionClass;
use Yii;

/**
 * 日志格式类
 * Class LogFormat
 * @package app\modules\v1\components
 */
class LogFormat
{
    //此次请求唯一id
    private $reqId;
    //记录行为
    private $op;
    //记录来源文件地址信息
    private $source;
    //记录来源文件代码所在行数
    private $line;
    //记录描述
    private $msg;


    public function __construct(string $op = "", string $source = "", string $line = "", string $msg = "")
    {
        $this->reqId = Yii::$app->request->getQueryParam("reqId");
        $this->op = $op;
        $this->source = $source;
        $this->line = $line;
        $this->msg = $msg;
    }

    public function format()
    {
        $reflect = new ReflectionClass($this);
        $props = $reflect->getProperties();
        $str = "";
        foreach ($props as $prop) {
            // 为了兼容反射私有属性
            $prop->setAccessible(true);
            // 当不想获取静态属性时
            if ($prop->isStatic()) {
                continue;
            }
            $str .= $prop->getName().":".$prop->getValue($this)."||";
        }
        if (!empty($str)) {
            $str = rtrim($str, "||");
        }
        return $str;
    }
}
